package com.example.teaching_server.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 统一API响应DTO
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@Schema(description = "统一API响应")
public class ApiResponse<T> {

    @Schema(description = "请求是否成功", example = "true", requiredMode = Schema.RequiredMode.REQUIRED)
    private Boolean success;

    @Schema(description = "响应消息", example = "操作成功", requiredMode = Schema.RequiredMode.REQUIRED)
    private String message;

    @Schema(description = "响应数据")
    private T data;

    @Schema(description = "错误代码")
    private String errorCode;

    @Schema(description = "时间戳", example = "2024-03-01T10:30:00Z")
    private String timestamp;

    /**
     * 为了兼容旧的isSuccess()调用，添加此方法
     */
    public boolean isSuccess() {
        return Boolean.TRUE.equals(success);
    }

    /**
     * 设置success字段的便捷方法
     */
    public void setSuccess(boolean success) {
        this.success = success;
    }

    // 成功响应（带数据）
    public static <T> ApiResponse<T> success(T data) {
        return success("操作成功", data);
    }

    // 成功响应（自定义消息）
    public static <T> ApiResponse<T> success(String message, T data) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setMessage(message);
        response.setData(data);
        response.setTimestamp(java.time.Instant.now().toString());
        return response;
    }

    // 失败响应
    public static <T> ApiResponse<T> fail(String message) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setSuccess(false);
        response.setMessage(message);
        response.setTimestamp(java.time.Instant.now().toString());
        return response;
    }

    // 失败响应（包含错误代码）
    public static <T> ApiResponse<T> fail(String message, String errorCode) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setSuccess(false);
        response.setMessage(message);
        response.setErrorCode(errorCode);
        response.setTimestamp(java.time.Instant.now().toString());
        return response;
    }

    // 成功响应（无数据）
    public static <T> ApiResponse<T> success() {
        return success("操作成功", null);
    }

    // 成功响应（仅消息）
    public static <T> ApiResponse<T> successMessage(String message) {
        return success(message, null);
    }

    /**
     * 验证响应是否包含数据
     */
    public boolean hasData() {
        return data != null;
    }

    /**
     * 获取数据或默认值
     */
    public T getDataOrDefault(T defaultValue) {
        return hasData() ? data : defaultValue;
    }

    /**
     * 手动实现Builder类
     */
    public static class ApiResponseBuilder<T> {
        private Boolean success;
        private String message;
        private T data;
        private String errorCode;
        private String timestamp;

        public ApiResponseBuilder<T> success(Boolean success) {
            this.success = success;
            return this;
        }

        public ApiResponseBuilder<T> message(String message) {
            this.message = message;
            return this;
        }

        public ApiResponseBuilder<T> data(T data) {
            this.data = data;
            return this;
        }

        public ApiResponseBuilder<T> errorCode(String errorCode) {
            this.errorCode = errorCode;
            return this;
        }

        public ApiResponseBuilder<T> timestamp(String timestamp) {
            this.timestamp = timestamp;
            return this;
        }

        public ApiResponse<T> build() {
            ApiResponse<T> response = new ApiResponse<>();
            response.setSuccess(this.success);
            response.setMessage(this.message);
            response.setData(this.data);
            response.setErrorCode(this.errorCode);
            response.setTimestamp(this.timestamp);
            return response;
        }
    }

    /**
     * 静态builder方法
     */
    public static <T> ApiResponseBuilder<T> builder() {
        return new ApiResponseBuilder<T>();
    }
}
